#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash build.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1

create_dir()
{
    local path=$1
    [[ "$path" =~ .*/$ ]] && path=${path%/*}
    if [[ ! -d "$path" && ! -f "$path" ]]; then mkdir -p $path; else path=$path`date "+%y%m%d%H%M%S"` && mkdir -p $path; fi
    echo $path
}

buildpath=$(create_dir $1)
installpath=$(create_dir $2)
#buildpath=$1
#installpath=$2

yum install -y \
  wget make m4 environment-modules systemd-devel patch autoconf \
  automake libtool python python-devel python-setuptools \
  python2-pip tar libatomic gcc-c++ zlib zlib-devel \
  gcc python python-devel systemd-devel cmake numactl
source /etc/profile
source /etc/profile.d/modules.sh
set -e
dep_dir=$PWD/"dependency"

module purge
module use $installpath

bash $dep_dir/gcc-10.3.1-2022.11-aarch64-linux.sh $buildpath $installpath
module load ${installpath}/gcc-10.3.1-2022.11-aarch64-linux/gcc_modulefiles

bash $dep_dir/openmpi-4.1.2.sh $buildpath $installpath
module load ${installpath}/openmpi-4.1.2/openmpi_modulefiles

bash $dep_dir/openblas-0.3.21.sh $buildpath $installpath
module load ${installpath}/openblas-0.3.21/openblas_modulefiles

bash $dep_dir/scalapack-2.2.0.sh $buildpath $installpath
module load ${installpath}/scalapack-2.2.0/scalapack_modulefiles

bash $dep_dir/libxc-4.3.4.sh $buildpath $installpath
module load ${installpath}/libxc-4.3.4/libxc_modulefiles

bash $dep_dir/fftw-3.3.10.sh $buildpath $installpath
module load ${installpath}/fftw-3.3.10/fftw_modulefiles

bash $dep_dir/hdf5-1.12.1.sh $buildpath $installpath
module load ${installpath}/hdf5-1.12.1/hdf5_modulefiles

bash $dep_dir/pnetcdf-1.12.3.sh $buildpath $installpath
module load ${installpath}/pnetcdf-1.12.3/pnetcdf_modulefiles

bash $dep_dir/netcdf-c-4.9.0.sh $buildpath $installpath
module load ${installpath}/netcdf-c-4.9.0/netcdf-c_modulefiles

bash $dep_dir/netcdf-fortran-4.6.0.sh $buildpath $installpath
module load ${installpath}/netcdf-fortran-4.6.0/netcdf-fortran_modulefiles

bash $dep_dir/abinit-9.6.2.sh $buildpath $installpath
